package com.longrise.android.byjk.plugins.tabfirst;

import android.text.TextUtils;

import com.longrise.LEAP.Base.Objects.EntityBean;
import com.longrise.android.byjk.R;
import com.longrise.android.byjk.common.UrlConstants;
import com.longrise.android.byjk.model.HomeMultipleItem;
import com.longrise.android.byjk.model.UserInfor;
import com.longrise.common.datasource.remote.LoadDataManager;
import com.longrise.common.utils.AppUtil;
import com.longrise.common.datasource.local.sp.CacheUtils;
import com.longrise.common.utils.NetUtil;
import com.longrise.common.utils.PrintLog;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by callxkj on 2017/2/22.
 * 首页presenter
 */

public class HomePresenter extends HomeContract.Presenter {
    private static final String TAG = "HomePresenter";
    private final String SIGNDIALOGTIME = "signdialogtime" + UserInfor.getInstance().getUserid();

    @Override
    protected void init() {
        mView.showLoadingDefaultPage();
        requestBananer(true);
        requestPageMenusData(true);
        requestCustomerModule(true);
        PrintLog.d(TAG, "init");
    }

    @Override
    void refreshData() {
        if (NetUtil.checkNetEnable()) {
            requestBananer(false);
            requestPageMenusData(false);
            requestCustomerModule(false);
            PrintLog.d(TAG, "refreshData");
        } else {
            mView.refreshComplete();
        }

    }

    /**
     * 首页弹框显示今天有多少用户已经签到等
     */
    @Override
    void requestSignScoredToday() {
        boolean b = toCheckIfShowSignDialog();
        if (!b) {
            return;
        }
        EntityBean bean = new EntityBean();
        bean.set("userid", UserInfor.getInstance().getUserid());
        bean.set("cardno", UserInfor.getInstance().getUsersfzh());
        EntityBean fbean = new EntityBean();
        fbean.set("bean", bean);
        LoadDataManager.getInstance().callService(null, UrlConstants.BaseUrl,
                "bbt_vip_sVipUserScoresbyTodayqd", fbean, new LoadDataManager.OnRequestCallListener() {

                    @Override
                    public void onSuccess(String key, String service, Object result) {
                        try {
                            EntityBean bean = (EntityBean) result;
                            String resultdesc = bean.getString("resultdesc");
                            int value = (Integer) bean.get("resultstate");
                            if (value == 1) {
                                EntityBean resultbean = bean.getBean("result");
                                String acquiredscore = resultbean.getString("acquiredscore");
                                String qdcounts = resultbean.getString("qdcounts");
                                String planstate = resultbean.getString("planstate"); //1在活动期间  0 不在活动期间
                                String qdstate = resultbean.getString("qdstate"); //1进入 还未签到  0已经签到过了
                                if ("1".equals(qdstate)) {
                                    mView.showSignDialog(acquiredscore, qdcounts, planstate);
                                }
                            } else {
                                mView.showToast(resultdesc);
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }

                    @Override
                    public void onFinished(String key, String service) {
                    }

                    @Override
                    public void onError(String key, String service, Throwable ex, boolean isOnCallback) {

                    }
                });

    }

    /**
     * 同一个用户每天只能弹出一次
     */
    private boolean toCheckIfShowSignDialog() {
        boolean flag;
        SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd");
        Date curdata = new Date(System.currentTimeMillis());
        String strTime = formatter.format(curdata);
        String spKey = SIGNDIALOGTIME;
        String newspValue = strTime + "_" + UserInfor.getInstance().getUserid();
        String spValue = CacheUtils.getString(spKey, null);
        if (TextUtils.isEmpty(spValue)) {
            CacheUtils.setString(spKey, newspValue);
            flag = true;
        } else {
            if (newspValue.equals(spValue)) { //相等 说明当天该用户弹出过签到弹窗
                flag = false;
            } else {
                CacheUtils.setString(spKey, newspValue);
                flag = true;
            }
        }
        return flag;
    }

    /**
     * 首页中部8个导航栏按钮
     */
    private void requestPageMenusData(boolean isfirst) {
        if (isfirst) {
            //第一次加载的时候先使用本地数据
            EntityBean[] bbt_sIndexPageMenus = CacheUtils.getCacheFirstData("bbt_sIndexGuideMenus");
            parseIndexGuideMenus(bbt_sIndexPageMenus);
        }

        EntityBean bean = new EntityBean();
        bean.set("apptype", "android");
        EntityBean fbean = new EntityBean();
        fbean.set("bean", bean);
        LoadDataManager.getInstance().callService(null, UrlConstants.BaseUrl,
                "bbt_sIndexGuideMenus", fbean, new LoadDataManager.OnRequestCallListener() {

                    @Override
                    public void onSuccess(String key, String service, Object result) {
                        try {
                            EntityBean bean = (EntityBean) result;
                            String resultdesc = bean.getString("resultdesc");
                            int value = (Integer) bean.get("resultstate");
                            if (value == 1) {
                                EntityBean[] resultBeans = bean.getBeans("result");
                                parseIndexGuideMenus(resultBeans);
                                CacheUtils.cacheFirstData("bbt_sIndexGuideMenus", resultBeans);
                            } else {
                                mView.showToast(resultdesc);
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }

                    @Override
                    public void onFinished(String key, String service) {
                    }

                    @Override
                    public void onError(String key, String service, Throwable ex, boolean isOnCallback) {

                    }
                });
    }

    /**
     * 解析首页中部8个导航栏按钮的数据
     */
    private void parseIndexGuideMenus(EntityBean[] bean) {
        if (bean == null) {
            return;
        }
        mView.refreshHead_rcv(bean);
    }


    /**
     * 首页底部免费试看,批量购买,保险头条的动态配置
     */
    private void requestCustomerModule(boolean isfirst) {
        if (isfirst) {
            //第一次加载的时候先使用本地数据
            EntityBean[] bbt_sIndexCustomerModule = CacheUtils.getCacheFirstData("bbt_sIndexCustomerModule");
            parseCustomerModuleBean(bbt_sIndexCustomerModule);
        }

        EntityBean bean = new EntityBean();
        bean.set("apptype", "android");
        EntityBean fbean = new EntityBean();
        fbean.set("bean", bean);
        LoadDataManager.getInstance().callService(null, UrlConstants.BaseUrl,
                "bbt_sIndexCustomerModule", fbean, new LoadDataManager.OnRequestCallListener() {

                    @Override
                    public void onSuccess(String key, String service, Object result) {
                        try {
                            EntityBean bean = (EntityBean) result;
                            int value = (Integer) bean.get("resultstate");
                            String resultdesc = bean.getString("resultdesc");
                            if (value == 1) {
                                EntityBean[] resultBeans = bean.getBeans("result");
                                if (resultBeans == null) {
                                    return;
                                }
                                parseCustomerModuleBean(resultBeans);
                                CacheUtils.cacheFirstData("bbt_sIndexCustomerModule", resultBeans);
                            } else {
                                mView.showToast(resultdesc);
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }

                    @Override
                    public void onFinished(String key, String service) {
                        mView.dismissLoadingDefaultPage();
                        mView.refreshComplete();
                    }

                    @Override
                    public void onError(String key, String service, Throwable ex, boolean isOnCallback) {
                        mView.showToast(AppUtil.getString(R.string.networkerror));
                    }
                });
    }


    /**
     * 解析新手专区,保宝头条这种item
     * style： 样式id(1001:类似新手专区TYPE1 ; 1002:类似保险头条TYPE2 ;)
     */
    private void parseCustomerModuleBean(EntityBean[] beans) {
        List<HomeMultipleItem> items = new ArrayList<>();
        if (beans == null || beans.length == 0) {
            return;
        }
        for (EntityBean bean : beans) {
            String style = bean.getString("style");
            switch (style) {
                case "1001": //类似新手专区
                    items.add(new HomeMultipleItem(HomeMultipleItem.TYPE1));
                    break;
                case "1002": //类似保险头条
                    items.add(new HomeMultipleItem(HomeMultipleItem.TYPE2));
                    break;
                case "1004": //宝易工具(一个区域)
                    items.add(new HomeMultipleItem(HomeMultipleItem.TYPE3));
                    break;
                case "1005": //宝易工具(三个区域)
                    items.add(new HomeMultipleItem(HomeMultipleItem.TYPE4));
                    break;
                case "1006": //宝易工具(五个区域)
                    items.add(new HomeMultipleItem(HomeMultipleItem.TYPE5));
                    break;
                case "1009": //免费试看
                    items.add(new HomeMultipleItem(HomeMultipleItem.TYPE7));
                    break;
                case "1010": //宣传视频
                    items.add(new HomeMultipleItem(HomeMultipleItem.TYPE8));
                    break;
                case "1011": //批量购买
                    items.add(new HomeMultipleItem(HomeMultipleItem.TYPE9));
                    break;
                case "1012": //hra1个免费名额
                    items.add(new HomeMultipleItem(HomeMultipleItem.TYPE10));
                    break;
                default:  //空样式 如果服务端传了一个新的样式 就用这个空样式
                    items.add(new HomeMultipleItem(HomeMultipleItem.TYPE_DEFAULT));
                    break;
            }
        }
        mView.refreshHome_rcv(beans, items);
    }

    /**
     * 首页banner
     */
    private void requestBananer(boolean isFirst) {
        if (isFirst) {
            //先读缓存 预加载
            EntityBean[] bbt_sIndexRollPics = CacheUtils.getCacheFirstData("bbt_sIndexRollPic");
            if (bbt_sIndexRollPics != null) {
                mView.refreshBanner(bbt_sIndexRollPics);
            }
        }
        EntityBean bean = new EntityBean();
        bean.set("apptype", "android");
        bean.set("positiontype",1);
        EntityBean fbean = new EntityBean();
        fbean.set("bean", bean);
        LoadDataManager.getInstance().callService(null, UrlConstants.BaseUrl,
                "bbt_sIndexRollPic", fbean, new LoadDataManager.OnRequestCallListener() {

                    @Override
                    public void onSuccess(String key, String service, Object result) {
                        try {
                            EntityBean bean = (EntityBean) result;
                            int value = (Integer) bean.get("resultstate");
                            String resultdesc = bean.getString("resultdesc");
                            if (value == 1) {
                                EntityBean[] resultBean = bean.getBeans("result");
                                if (resultBean != null) {
                                    CacheUtils.cacheFirstData("bbt_sIndexRollPic", resultBean);
                                    mView.refreshBanner(resultBean);
                                }
                            } else {
                                mView.showToast(resultdesc);
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }

                    @Override
                    public void onFinished(String key, String service) {
                    }

                    @Override
                    public void onError(String key, String service, Throwable ex, boolean isOnCallback) {
                    }
                });
    }


    /**
     * 获取首页的广告地址
     */
    public void getHomeAdaddr() {
        EntityBean bean = new EntityBean();
        bean.set("apptype", "android");
        bean.set("positiontype",2);
        EntityBean fbean = new EntityBean();
        fbean.set("bean", bean);
        LoadDataManager.getInstance().callService(null, UrlConstants.BaseUrl,
                "bbt_sIndexRollPic", fbean, new LoadDataManager.OnRequestCallListener() {

                    @Override
                    public void onSuccess(String key, String service, Object result) {
                        try {
                            EntityBean bean = (EntityBean) result;
                            int value = (Integer) bean.get("resultstate");
                            String resultdesc = bean.getString("resultdesc");
                            if (value == 1) {
                                EntityBean[] resultBean = bean.getBeans("result");
                                if (resultBean != null) {
                                    CacheUtils.cacheFirstData("bbt_sIndexRollPic", resultBean);
                                }
                                mView.toShowAd(resultBean);
                            } else {
                                mView.showToast(resultdesc);
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }

                    @Override
                    public void onFinished(String key, String service) {
                    }

                    @Override
                    public void onError(String key, String service, Throwable ex, boolean isOnCallback) {
                    }
                });
    }




}
